<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单查询控制器
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 */
quanli_require('module/base/company/companyMaster.class.php');
quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/system/sysDepartment.class.php');
quanli_require('module/base/system/sysUser.class.php');

class pa_billSearch {
	
    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //订单查询页面
                    $this->searchBill();
                    break;

                case 2: //澳粒订单查询页面
                    $this->AL_searchBill();
                    break;
                //add by allen.qiang  <allen.qiang@fistforward.com> 2015-01-21 allen.qiang FUN-AC-0028-下午茶项目 start
                case 3: //下午茶订单查询页面
                    $this->XWC_searchBill();
                    break;
                //add by allen.qiang  <allen.qiang@fistforward.com> 2015-01-21 allen.qiang FUN-AC-0028-下午茶项目 end
                case 9: //订单详情
                    $this->billDetail();
                    break;

                case 10: //试吃订单查询
                    $this->sampleOrder();
                    break;


                default:
                    backendError('参数错误');
                    break;

            }
        } else {

            backendError('参数错误');

        }

    }

    //试吃订单查询
    private function sampleOrder(){
        global $G_PGLOB;


        //实例用户类
        $sysUser = new sysUser( $G_PGLOB->DBCA );

        //当前登录用户信息
        $args_array = array(
            'userID' => $G_PGLOB->currentUserID
        );
        $userInfo_array = $sysUser->searchUserInfo( $args_array );
        $cityID_1=$userInfo_array[0]['cityID'];
        $departmentID = $userInfo_array[0]['departmentID'];
        $isSalesman = $userInfo_array[0]['isSalesman'];
        if ( empty( $departmentID ) || !is_numeric( $departmentID )) {
            backendError('您不属于任何部门，无权查看');
        }
        if ( empty( $isSalesman ) || !is_numeric( $isSalesman )) {
            backendError('您不是销售员，无权查看');
        }
        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();
        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1, $departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(60000));




        if (isset($_REQUEST['custID']) && !empty($_REQUEST['custID'])) {
            $G_PGLOB->smarty->assign('custID', $_REQUEST['custID']);
        }

        $G_PGLOB->smarty->assign_by_ref('city_array', $G_PGLOB->getCache('city'));

        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(1009));



        //根据当前登录者获取对应的城市ID （字符串 1,2,3,4）
        $G_PGLOB->smarty->assign('city_id',$cityID_1);



        $G_PGLOB->smarty->display('bill/billSample.tpl');


    }


    private function searchBill() {

        global $G_PGLOB;

        if (isset($_REQUEST['custID']) && !empty($_REQUEST['custID'])) {
            $G_PGLOB->smarty->assign('custID', $_REQUEST['custID']);
        }

        $table_id = 1;

        $flex_array = array(
            array('display' => '操作','width' => 200,'align' => 'center'),
            array('display' => '订单编号','name' => 'bill_no', 'width' => 260, 'sortable' => true, 'align' => 'center'),
            array('display' => '订单类型','name' => 'billType', 'width' => 100, 'sortable' => true),
            array('display' => '订单状态','width' => 80),
            array('display' => '订单标识','width' => 80),
            array('display' => '支付方式','width' => 120),
            array('display' => '代付订单号','width' => 200),
            array('display' => '到货时间','width' => 200),
            array('display' => '下单时间','width' => 140,'align' => 'center'),
            array('display' => '商品明细','width' => 300),
            array('display' => '商品数量','width' => 80),
            array('display' => '餐具数量','width' => 80),
            array('display' => '蜡烛数量','width' => 80),
            array('display' => '订单金额','width' => 80),
            array('display' => '应收金额','width' => 80),
            array('display' => '已收金额','width' => 80),
            array('display' => '调价金额','width' => 80),
            array('display' => '退货金额','width' => 80),
            array('display' => '配送自购金额','width' => 100),
            array('display' => '服务费','width' => 60),
            array('display' => '城市','width' => 80),
            array('display' => '区','width' => 80),
            array('display' => '配送站', 'width' => 120),
            array('display' => '配送员', 'width' => 120),
            array('display' => '配送员手机','width' => 120),
            array( 'display' => '收货人手机号码','width' => 120,'align' => 'center'),
            array('display' => '收货人','width' => 120),
            array('display' => '创建人','width' => 120),
            array('display' => '退货状态','width' => 100),
            array('display' => '发票类型', 'width' => 100),
            array('display' => '抬头类型','width' => 100),
            array('display' => '发票抬头','width' => 100),
            array('display' => '发票金额','width' => 100),
            array('display' => '发票内容','width' => 100),
            array('display' => '发 票 号','width' => 100),
        ); //flexigrid插件表格  end

        quanli_require('module/base/system/sysFlexigridLog.class.php');

        $sysFlexigridLog = new sysFlexigridLog();

        $arg_array = array(
            'table_id' => $table_id,
            'userID' => $G_PGLOB->currentUserID,
            'sortname' => 'last_cid',
            'sortorder' => 'ASC'
        );
        $flexigrid_array = $sysFlexigridLog->searchFlexigridInfo($arg_array);

        if($flexigrid_array){
            $flex_array = $flexigrid_array;
        }else{
            $arg_array = array(
                'table_id' => $table_id,
                'userID' => $G_PGLOB->currentUserID,
                'data' => $flex_array
            );
            $sysFlexigridLog->editFlexigrid($arg_array);
        }

        $G_PGLOB->smarty->assign_by_ref('table_id', $table_id);
        $G_PGLOB->smarty->assign_by_ref('fixed_json', json_encode($flex_array));
        $G_PGLOB->smarty->assign_by_ref('city_array', get_city());
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(1009));

        $G_PGLOB->smarty->display('bill/billSearch.tpl');

    }

    /**
     * 澳粒订单查询
     * @author create by allen.qiang
     */
    private function AL_searchBill(){

        global $G_PGLOB;

        if (isset($_REQUEST['custID']) && !empty($_REQUEST['custID'])) {
            $G_PGLOB->smarty->assign('custID', $_REQUEST['custID']);
        }

        $G_PGLOB->smarty->assign_by_ref('city_array', $G_PGLOB->getCache('city'));
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(1009));

        $G_PGLOB->smarty->display('bill/AL_billSearch.tpl');
    }

    /**
     * 订单详情页面
     * @author create by allen.qiang
     */
    private function billDetail() {

        if(!isset($_REQUEST['bill_no']) || empty($_REQUEST['bill_no'])){
            backendError('缺少必要的参数');
        }

        global $G_PGLOB;

        $bill_no = $_REQUEST['bill_no'];

        quanli_include('module/base/customer/logHandler.class.php');
        quanli_require('module/base/bill/billSearch.class.php');
        quanli_require('module/base/customer/quanliCust.class.php');
        quanli_include('module/base/company/companyMaster.class.php');


        //实例日志类
        $logHandler = new logHandler( $G_PGLOB->DBCA );

        //实例订单类
        $billSearch = new billSearch( $G_PGLOB->DBCA );

        //实例会员类
        $quanliCust = new quanliCust();

        //实例化大客户类
        $companyMaster = new companyMaster();

        //查询参数
        $arg_array = array(
            'bill_no' => $bill_no,
            'sortname' => 'createTime',
            'sortorder' => 'DESC'
        );

        $bill_array = $billSearch->searchBillInfo( $arg_array );

        if (!$bill_array) {
            backendError('订单信息没有找到');
        }

        $bill_array = $bill_array[0];

        //异地付款
        $bill_other_address_pay = array();
        if ($bill_array['special_status'] == 3) {
            $bill_other_address_pay = $billSearch->searchBillOtherAddressPay($arg_array);
        }

        //订单支付方式
        $bill_pay_array = $billSearch->searchBillPay( $arg_array );

        //优惠券
        $bill_coupon_array = $billSearch->searchBillUsedCoupon( $bill_no );

        //代金卡
        $bill_card_array = $billSearch->searchBillUsedCard( $bill_no );

        //订单轨迹
        $bill_trace_array = $billSearch->searchBillTrace( $arg_array );

        //内部留言
        $bill_remark_array = $billSearch->searchBillRemark( $arg_array );

        //订单操作记录
        $arg_array['logCategoryID'] = 1;
        $arg_array['logItem'] = $bill_no;
        //add by allen.qiang 20150512 订单详情页操作日志排序 start
        //MOD BY ALLEN.QIANG 20150611 排序字段使用自增长列 START
        $arg_array['sortname'] = 'detail_id';
        $arg_array['sortorder'] = 'DESC';
        //MOD BY ALLEN.QIANG 20150611 排序字段使用自增长列 END
        //add by allen.qiang 20150512 订单详情页操作日志排序 end
        $bill_log_array = $logHandler->searchLogInfo( $arg_array );

        $args_array = array(
            'pcustID' => $bill_array['pcustID']
        );

        //会员信息
        $customer_array = $quanliCust->searchCustomerDetail( $args_array );

        //大客户信息
        $companyMaster_array = $companyMaster->search($args_array);
        if($companyMaster_array){
            $companyMaster_array = $companyMaster_array[0];
        }
        
        //add by yim.hu FUN-BK-0009  获取促销渠道 start
        $bill_promotion = $billSearch->searchPromotion($bill_array['billPromotion']);
        if(isset($bill_promotion[0]->itemName)){
        	$bill_promotion = $bill_promotion[0]->itemName;
        }else{
        	$bill_promotion ='无';
        }
        //add by yim.hu FUN-BK-0009  获取促销渠道 end
        //查找订单商品
        $arg_array = array(
            'bill_no' => $bill_no
        );
        $bill_goods_array = $billSearch->searchBillGoods( $arg_array );

        if($bill_goods_array){
            foreach($bill_goods_array as &$goods){
                $arg_array = array(
                    'bill_goods_no' => $goods['bill_goods_no']
                );
                $goods['fittings'] = $billSearch->searchGoodsFittingsBill( $arg_array );

            }
        }

        //alter by xuhongshun 查找代付订单信息 与被代付订单信息 start

        //订单代付信息
        $arg_array = array(
            'main_bill_no'=>$bill_no
        );

        $billPayingAgent = $billSearch->searchBillPayingAgent( $arg_array );
        if( $billPayingAgent ){
            $G_PGLOB->smarty->assign_by_ref('billPayingAgent', $billPayingAgent);
        }

        //被订单代付信息
        $arg_array = array(
            'bill_no'=>$bill_no
        );

        //查询代付订单表
        $billPayingAgentBy = $billSearch->searchBillPayingAgent( $arg_array );

        if( $billPayingAgentBy ){
            foreach( $billPayingAgentBy as $val ){

                //查询订单主表bill_master 获得被代付订单编号
                $arg_array = array(
                    'bill_no'=>$val['main_bill_no']
                );

                $bill_master_array = $billSearch->searchBillInfo( $arg_array );
                if( $bill_master_array ){
                    $temp_array = array(
                        'bill_code'=>$bill_master_array[0]['bill_code'],
                        'bill_no'=>$val['main_bill_no']
                    );
                    $G_PGLOB->smarty->assign_by_ref('temp_array', $temp_array);
                }
            }
        }
        //add by JunHui.Li FUN-BK-0051 20150212 增加会员折扣 start
        $search_sql = 'SELECT rate FROM cust_rank where rankID= ?';
        $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, 's',$customer_array['rankID']);
        $customer_array['rate']=isset($temp_item_array[0]->rate)?$temp_item_array[0]->rate:'1.00';
        //add by JunHui.Li FUN-BK-0051 20150212 增加会员折扣 end
        $G_PGLOB->smarty->assign_by_ref('bill_array', $bill_array);
        $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);
        $G_PGLOB->smarty->assign_by_ref('companyMaster_array', $companyMaster_array);
        $G_PGLOB->smarty->assign_by_ref('bill_pay_array', $bill_pay_array);
        $G_PGLOB->smarty->assign_by_ref('bill_coupon_array', $bill_coupon_array);
        $G_PGLOB->smarty->assign_by_ref('bill_card_array', $bill_card_array);
        $G_PGLOB->smarty->assign_by_ref('bill_other_address_pay', $bill_other_address_pay[0]);
        $G_PGLOB->smarty->assign_by_ref('bill_trace_array', $bill_trace_array);
        $G_PGLOB->smarty->assign_by_ref('bill_remark_array', $bill_remark_array);
        $G_PGLOB->smarty->assign_by_ref('bill_log_array', $bill_log_array);
        $G_PGLOB->smarty->assign_by_ref('bill_goods_array', $bill_goods_array);

        $G_PGLOB->smarty->assign_by_ref('bill_promotion', $bill_promotion);
        $G_PGLOB->smarty->display('bill/billDetail.tpl');

    }

    /**
     * 下午茶订单查询
     * @author allen.qiang  <allen.qiang@fistforward.com> 2015-01-20 allen.qiang FUN-AC-0028-下午茶项目
     */
    private function XWC_searchBill(){

        global $G_PGLOB;

        if (isset($_REQUEST['custID']) && !empty($_REQUEST['custID'])) {
            $G_PGLOB->smarty->assign('custID', $_REQUEST['custID']);
        }

        $G_PGLOB->smarty->assign_by_ref('city_array', get_city());
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(1009));

        $G_PGLOB->smarty->display('bill/XWC_billSearch.tpl');
    }

}
